package com.aaa.controller.tables;

import com.aaa.dao.BookMapper;
import com.aaa.service.BookService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wsc.entity.Book;
import com.wsc.entity.Type;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("book")
public class BookController {
    @Resource
    BookMapper bookMapper;
    @Resource
    BookService bookService;
    @RequestMapping("select")
    public Object select(){
        return bookMapper.selectList(null);
    }
    @RequestMapping("insert")
    public Integer insert(Book book){
        return bookMapper.insert(book);
    }
    @RequestMapping("delete")
    public Integer delete(Integer id){
        return bookMapper.deleteById(id);
    }
    @RequestMapping("update")
    public Integer update(Book book){
        return bookMapper.updateById(book);
    }
    @RequestMapping("select2")
    public Object select2(Integer pageCode, Integer pageSize, Book book){
        PageHelper.startPage(pageCode,pageSize);
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.like("name",book.getName());
        queryWrapper.like("state",book.getState());
        List<Type> list = bookMapper.selectList(queryWrapper);
        PageInfo<Type> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }
    @RequestMapping("updateS")
    public Object updateS(Book book){
//        QueryWrapper queryWrapper = new QueryWrapper();
//        queryWrapper.like("id",book.getId());
//        UpdateWrapper updateWrapper=new UpdateWrapper();
//        updateWrapper.set("state",book.getState());
         return new LambdaUpdateChainWrapper<Book>(bookMapper)
                .eq(Book::getId, book.getId())
                .set(Book::getState, book.getState())
                .update()
                ;
    }
    @RequestMapping("select3")
    public Object select3(){
    //        System.out.println(bookService.select3()+"***************************");
        return bookService.select3();
    }

    @RequestMapping("select4")
    public Object select4(Book book){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.like("name",book.getName());
        queryWrapper.like("price",book.getPrice());
        queryWrapper.like("brief",book.getBrief());
        queryWrapper.like("detail",book.getDetail());
        queryWrapper.like("state",book.getState());
        List<Type> list = bookMapper.selectList(queryWrapper);
        PageInfo<Type> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }
}
